package 力扣日常刷题.木22一月.第15天0127;

/**
 * @author 帅小伙
 * @date 2022/1/27
 * @description 过关斩将   逐一判断
 */
public class Demo01模拟句子中的有效单词数 {


    public int countValidWords(String sentence) {
        String[] strings = sentence.split(" ");
        int ans = 0;
        for (String word : strings) if (check(word)) ans++;
        return ans;
    }

    boolean check(String s) {
        int n = s.length();
        if (n == 0) return false;
        for (int i = 0, c1 = 0, c2 = 0; i < n; i++) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) return false;
            if (c == ' ') return false;
            if (c == '-' && ++c1 >= 0) {
                //一个 收尾
                if(c1 > 1 || (i == 0 || i == n-1))return false;
                // - 两端都是小写字母
                if (!Character.isLetter(s.charAt(i - 1)) || !Character.isLetter(s.charAt(i + 1))) return false;
            }
            if ((c == '!' || c == '.' || c == ',') && ++c2 >= 0) {
                if (c2 > 1 || (i != n - 1)) return false;
            }
        }
        return true;
    }

}
